package com.bankcomm.ui.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 * LPCityGridView.java
 * 
 * @author cheng.l 2013-5-16 上午09:37:08
 */
// 自定义GridView
public class LPCityGridView extends GridView {
	private Paint paint;
	private int columns = 3;
	private boolean isDrawLine = true;
	public LPCityGridView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	public LPCityGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(0xffABB8C0);
	}

	/**
	 * 设置GridView列数
	 * @param columns 
	 * */
	public void setColumns(int columns){
		this.columns = columns;
		this.setNumColumns(columns);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		// 设置GridView不滚动
		int expandSec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSec);
	}

	/**
	 * 是否画横线
	 * @param yes
	 */
	public void drawableHorizontalLine(boolean yes){
		isDrawLine = yes;
	}
	
    @Override
    protected void dispatchDraw(Canvas canvas) {
    	if(isDrawLine){
    		final int count = getChildCount();
    		final int top = count > 0 ? getChildAt(0).getTop() : 0;
    		final int mHeight = count > 0? getChildAt(0).getHeight() : 40;
    		
    		final int width = getWidth();
    		final int height = getHeight();
    		
    		for (int y = top; y < height; y += mHeight) {
    			int index = y / mHeight;
    			int limit = count%columns == 0 ?  (count/columns) : (count/columns+1);
    			if(index > 0 && index < limit){
    				canvas.drawLine(0, y, width, y, paint);        		
    			}
    		}    		
    	}
        super.dispatchDraw(canvas);
    }
}
